package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.OrderItems;
import com.addplus.townmall.server.api.model.town.expand.OrderExpand;
import com.addplus.townmall.server.api.model.town.expand.OrderItemsExpand;
import com.addplus.townmall.server.api.model.town.param.OrderShopItemsParam;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单子项详情表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-09-30
 */
public interface OrderItemsMapper extends BaseAddMapper<OrderItems> {


    @Select("<script>\n" +
            "SELECT\n" +
            " p.store_id AS shop_store_id,\n" +
            " p.id AS shop_product_id,\n" +
            " p.shop_name,\n" +
            " p.shop_seal_img,\n" +
            " p.shop_fright_id,\n" +
            " p.fright_type,\n" +
            " p.is_discount,\n" +
            " p.shop_customize_code,\n" +
            " p.online_status,\n" +
            " p.sale_status,\n" +
            " p.store_id,\n" +
            " p.shop_tag,\n" +
            " sku.sale_price,\n" +
            " sku.cost_price,\n" +
            " sku.line_price AS shop_price,\n" +
            " sku.stock,\n" +
            " sku.id AS shop_specification_sku_id,\n" +
            " sku.sku_id AS shop_specification_sku_sku_id,\n" +
            " sku.sku_describe AS shop_specification_sku_json,\n" +
            " IFNULL( c.enable_noreason_return, 0 ) AS enable_noreason_return, \n" +
            " a.shop_active_type,\n" +
            " a.shop_obj_id,\n" +
            " a.status \n" +
            "FROM\n" +
            " shop_product p\n" +
            " LEFT JOIN shop_specification_sku sku ON sku.shop_product_id = p.id \n" +
            " AND sku.is_deleted = 0\n" +
            " LEFT JOIN shop_participate_active a ON sku.shop_product_id = a.shop_product_id \n" +
            " AND a.shop_sku_id = sku.id \n" +
            " AND a.`status` = 1 \n" +
            " AND a.is_deleted = 0\n" +
            " LEFT JOIN platform_category c ON p.shop_category_id = c.id \n" +
            " AND c.is_deleted = 0 \n" +
            "WHERE\n" +
            " sku.id IN \n" +
            " <foreach collection='skuIdList' open='('  item='item'  separator=',' close=')'> #{item} </foreach>\n" +
            " AND p.audit_status = 0 AND p.is_deleted = 0 AND p.sale_status = 0\n" +
            "</script>")
    @MapKey("shopSpecificationSkuId")
    Map<Integer, OrderShopItemsParam> getConfirmOrderItemsMessage(@Param("skuIdList") List<Integer> skuIdList);


    @Select("SELECT\n" +
            " id,\n" +
            " order_id,\n" +
            " store_id,\n" +
            " shop_product_id,\n" +
            " shop_name,\n" +
            " shop_seal_img,\n" +
            " sale_price,\n" +
            " shop_tag,\n" +
            " purchase_quantity,\n" +
            " shop_specification_sku_id,\n" +
            " shop_specification_sku_json,\n" +
            " enable_noreason_return,\n" +
            " gmt_modified \n" +
            "FROM\n" +
            " order_items \n" +
            "WHERE\n" +
            " order_id = #{orderId} \n" +
            " AND is_delete = 0 \n" +
            "ORDER BY\n" +
            " id")
    List<OrderShopItemsParam> getConirmCreateOrderItems(@Param("orderId") Integer orderId);


    @Select("SELECT\n" +
            " id,\n" +
            " order_id,\n" +
            " store_id,\n" +
            " shop_product_id,\n" +
            " shop_name,\n" +
            " shop_seal_img,\n" +
            " sale_price,\n" +
            " purchase_quantity,\n" +
            " shop_specification_sku_id,\n" +
            " shop_specification_sku_json,\n" +
            " shop_tag,\n" +
            " enable_noreason_return,\n" +
            " is_refund,\n" +
            " gmt_modified \n" +
            "FROM\n" +
            " order_items \n" +
            "WHERE\n" +
            " order_id = #{orderId} \n" +
            " AND is_delete = 0 \n" +
            "ORDER BY\n" +
            " id")
    List<OrderItems> getOrderItemsDetail(@Param("orderId") Integer orderId);


    @Select("SELECT\n" +
            " count( i.purchase_quantity ) AS purchase_quantity \n" +
            "FROM\n" +
            " order_items i\n" +
            " LEFT JOIN town_order o ON i.order_id = o.id \n" +
            " AND o.is_delete = 0 \n" +
            " AND o.order_status != 6 \n" +
            "WHERE\n" +
            " shop_active_type = #{shopActiveType} \n" +
            " AND shop_active_id = #{shopActiveId} \n" +
            " AND o.is_delete = 0")
    Integer getShopParticipageActiveCount(@Param("shopActiveType") Integer shopActiveType, @Param("shopActiveId") Integer shopActiveId);


    @Select("select sum(cost_price*purchase_quantity) as sumCostPrice from order_items WHERE order_id=#{orderId} ")
    BigDecimal getSumCostPrice(@Param("orderId") Integer orderId);

    @Select("<script>\n" +
            "SELECT *" +
            " from order_items  a " +
            "where " +
            "a.id in " +
            "<foreach collection='Items' open='('  item='item'  separator=',' close=')'> #{item}</foreach> " +
            "and order_id=#{orderId}" +
            "</script>")
    List<OrderItems> getItemsByIds(@Param("Items") List<String> areaIdList, @Param("orderId") Integer orderId);


    @Select("SELECT\n" +
            " o.id,\n" +
            " o.icon_path,\n" +
            " o.store_name,\n" +
            " o.order_number,\n" +
            " o.order_status,\n" +
            " o.pay_status,\n" +
            " o.freight,\n" +
            " o.actual_pay_amount,\n" +
            " o.shop_total_num\n" +
            "FROM\n" +
            " town_order o\n" +
            "WHERE\n" +
            " o.id = #{orderId} " +
            " AND o.member_id = #{memberId}\n" +
            " AND o.is_delete = 0")
    OrderExpand getOrderRefundById(@Param("memberId") Integer memberId, @Param("orderId") Integer orderId);

    @Select("<script>\n" +
            "SELECT\n" +
            " o.id,\n" +
            " o.order_id,\n" +
            " o.shop_product_id,\n" +
            " o.shop_product_type,\n" +
            " o.shop_name,\n" +
            " o.shop_seal_img,\n" +
            " o.sale_price,\n" +
            " o.purchase_quantity,\n" +
            " o.shop_specification_sku_json,\n" +
            " o.enabled_refund_amount,\n" +
            " o.shop_tag \n" +
            "FROM\n" +
            " order_items o \n" +
            "WHERE\n" +
            " o.id IN " +
            "<foreach collection='orderItems' open='('  item='item'  separator=',' close=')'> #{item}</foreach> \n" +
            " AND o.order_id = #{orderId} \n" +
            " AND o.is_delete = 0 AND o.is_refund = 0 " +
            "</script>\n")
    List<OrderItems> getOrderItemsCanRefundByOrderItemsList(@Param("orderId") Integer orderId, @Param("orderItems") List<Integer> orderItems);

    @Select("SELECT\n" +
            " o.id,\n" +
            " o.order_id,\n" +
            " o.shop_product_id,\n" +
            " o.shop_product_type,\n" +
            " o.shop_name,\n" +
            " o.shop_seal_img,\n" +
            " o.sale_price,\n" +
            " o.purchase_quantity,\n" +
            " o.enabled_refund_amount,\n" +
            " o.shop_specification_sku_json,\n" +
            " o.shop_tag,\n" +
            " o.is_refund,\n" +
            " tor.order_status\n" +
            "FROM\n" +
            " order_items o\n" +
            " LEFT JOIN town_order tor ON tor.id = o.order_id \n" +
            " AND tor.is_delete = 0\n" +
            "WHERE\n" +
            " o.order_id = #{orderId} \n" +
            " AND tor.member_id = #{memberId} \n" +
            " AND o.is_delete = 0\n" +
            "ORDER BY o.id")
    List<OrderItemsExpand> getOrderItemsCanRefundingList(@Param("memberId") Integer memberId, @Param("orderId") Integer orderId);

    @Select("SELECT\n"+
                   "*"+
                   "FROM\n"+
                   " order_items \n"+
                   "WHERE\n"+
                   " order_id = #{orderId} \n"+
                   " AND is_delete = 0 \n"+
                   "ORDER BY\n"+
                   " id")
    List<OrderItems> getOrderItems(@Param("orderId") Integer orderId);

    @Select("select * from order_items \n" +
            "where \n" +
            "${ew.sqlSegment}")
    List<OrderItemsExpand> selectOrderList(@Param("ew")QueryWrapper queryWrapper);


}
